import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import * as actions from '../actions.js';
import * as Status from '../status';


class Weather extends Component {
	render() {
		const {status, city, weather, lowTemp, highTemp, dispatch} = this.props;
		switch(status) {
			case Status.LOADING:
				return (
					<div>天气信息请求中...</div>
				);
			case Status.SUCCESS:
				return (
					<div>
					{city} {weather} 最低气温 {lowTemp} 最高气温 {highTemp}
					</div>
				);
			case Status.FAILURE:
				return (
					<div>天气信息装载失败</div>
				);
			default:
				throw new Error('Unexpected status' + status);

		}
	}

	componentDidMount() {
		// 这里暂时硬编码了
		this.props.dispatch(actions.fetchWeather(101210904));
	}
}


Weather.propTypes = {};

function mapStateToProps(state, ownProps) {
	const weatherInfo = state.weather
	return {
		status: state.weather.status,
		city: weatherInfo.city,
		weather: weatherInfo.weather,
		lowTemp: weatherInfo.temp1,
		highTemp: weatherInfo.temp2
	};
}

export default connect(mapStateToProps)(Weather);
			